/*
 * Main.fx
 *
 * Created on Aug 14, 2009, 1:56:17 PM
 */

package foldervisualizer;

import javafx.stage.Stage;
import javafx.scene.Scene;
import nl.foldervisualizer.*;
import javafx.scene.chart.PieChart;
import java.lang.Math;
import javafx.scene.control.TextBox;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javax.swing.JFileChooser;
import java.util.List;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.VBox;

/**
 * @author paul
 */

var folder : Folder;
var visualizer : FolderVisualizer;
var visitedDirs : String[];

function createSequence(list : List) : FolderItem[]{
    var items : FolderItem[] = [];

    for(item in list) {
        insert item as FolderItem into items;
    };

    return items;
}

var scene : Scene;
var dirNameInput : TextBox;

function visualize(): Void{
   
    var visualizeTask = VisualizeTask {
        dir: dirNameInput.text
        callback: FXListener {
            override function callback(ret : List) : Void{                
                createChart(createSequence(ret));
            }
        }
    };

    delete scene.lookup("chart")  from scene.content;
        
    visualizeTask.start();

    insert ProgressIndicator {
             id: "progress"
             progress: -1;
             translateX: 300
             translateY: 400
             scaleX: 10
             scaleY: 10
           } into scene.content;    
}

function createChart(items : FolderItem[]) : Void {
    var chart = PieChart {
        id: "chart"
             height: 600
             width: 600
             translateY:100

            data: bind [
                for(item in items) {
                    var fi = item as FolderItem;

                    PieChart.Data {
                        value: Math.round(fi.getRelativeSize())
                        label: fi.getItemName()
                        action: function() {
                            dirNameInput.text = fi.getPath();
                            insert fi.getItemName() into visitedDirs;
                            visualize();
                        }

                    }
                }

            ]
        }

        delete scene.lookup("progress") from scene.content;
        

        insert chart into scene.content;
}



Stage {
    title: "Folder Visualizer"
    width: 800
    height: 600
    scene: scene = Scene {
        content: [
            VBox {
                content: [
                    BreadCrum {
                        folders: bind visitedDirs
                        itemClicked: function(item, list) {
                            dirNameInput.text = item;
                            visitedDirs = list;
                            visualize();
                        }

                    }


                    HBox {
                        content: [
                            dirNameInput = TextBox {

                                text: "Choose a dir"
                                columns: 30
                                selectOnFocus: true
                            }

                            Button {
                                text: "Browse..."
                                action: function() {
                                    var fc = new JFileChooser();
                                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

                                    var retVal = fc.showOpenDialog(null);

                                    if(retVal == JFileChooser.APPROVE_OPTION) {
                                        dirNameInput.text = fc.getSelectedFile().getAbsolutePath();
                                        visitedDirs = dirNameInput.text;
                                        visualize();
                                    }

                                }
                            }


                        ]
                    }
                ]
            }




          
        ]
    }
}

